#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i ++) cin >> a[i];
  const int inf = 1e9;
  vector<vector<int>> dp(n + 2, vector<int>(n + 2));
  auto is_palindrome = [&](int l, int r) -> bool {
    while (l < r) {
      if (a[l] != a[r]) return false;
      l ++;
      r --;
    }
    return true;
  };
  for (int l = n; l >= 1; l --) {
    for (int r = l; r <= n; r ++) {
      dp[l][r] = inf;
      if (is_palindrome(l, r)) {
        dp[l][r] = 1;
      }
      for (int k = l; k < r; k ++) {
        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
      }
      if (a[l] == a[r] && l + 1 <= r - 1) {
        dp[l][r] = min(dp[l][r], dp[l + 1][r - 1]);
      }
    }
  }
  cout << dp[1][n] << endl;
}